import { usePageRequest, useRequest } from "../../../utils/request";
import dayjs from "../../../utils/day";
import { addDynamicHistory } from "../../../utils/history";
import { wxPromise } from "../../../utils/utils";
const windowWidth = wx.getSystemInfoSync().windowWidth

function dateFormat(data) {
    data.list = data.list.map(item => {
        item.date = dayjs.unix(item.public_time).format('YYYY-MM-DD')
        return item
    })
    return data
}

Component({
    properties: {},
    data: {
        current: 0,
        windowWidth: windowWidth,
        height: 300,
    },
    methods: {
        onLoad(options) {
            const { id, type } = options
            this.setData({
                id,
                type
            })

            if (Number(type) === 1) {
                this.request = new useRequest('api/infodetail', { hold: true, data: 'data' }, this)
                this.commentRequest = new usePageRequest('api/infotalklist', {
                    data: 'commentData',
                    params: { info_id: id },
                    pretreatment: dateFormat
                }, this)
                this.reRequest = new useRequest('api/publicinfotalk', { hold: true }, this)
                this.deleteRequest = new useRequest('api/userdeleteinfo', {
                    params: { info_id: id },
                    hold: true
                })
            } else {
                this.request = new useRequest('api/traveldetail', { hold: true, data: 'data' }, this)
                this.commentRequest = new usePageRequest('api/traveltalklist', {
                    hold: true,
                    data: 'commentData',
                    params: { travel_id: id },
                    pretreatment: dateFormat
                }, this)
                this.reRequest = new useRequest('api/publictraveltalk', { hold: true }, this)
                this.deleteRequest = new useRequest('api/userdeletetravel', {
                    params: { travel_id: id },
                    hold: true
                })
            }

            this.favoriteRequest = new useRequest('api/collectall', {
                params: { type: 4, travel_id: id },
                loading: false,
                hold: true
            }, this)

            this.unFavoriteRequest = new useRequest('api/cancercollectdata', {
                params: { type: 4, travel_id: id },
                loading: false,
                hold: true
            }, this)

            this.request.run({
                info_id: id,
                travel_id: id
            }).then(data => {
                this.getBannerSize(data)
                if (Number(type) === 2) {
                    addDynamicHistory({
                        travel_id: id,
                        imgs: data.imgs,
                        title: data.content,
                        user_name: data.user_name,
                        user_avatar: data.user_avatar
                    })
                }
            })
            this.commentRequest.next()
        },
        onShareAppMessage() {
            return {
                title: '快来看看旅行中超棒的游记推荐吧',
                path: '/pages/dynamic/detail/detail?id=' + this.data.id + '&type=' + this.data.type
            }
        },
        onReachBottom() {
            this.commentRequest.next()
        },
        back() {
            wx.navigateBack()
        },
        favorite() {
            if (this.data.data.is_collect) {
                this.unFavoriteRequest.run().then(() => {
                    this.setData({
                        ['data.is_collect']: false
                    })
                })
            } else {
                this.favoriteRequest.run().then(() => {
                    this.setData({
                        ['data.is_collect']: true
                    })
                })
            }
        },
        bannerChange(e) {
            this.setData({
                current: e.detail.current
            })
        },
        delete() {
            wx.showModal({
                title: '提示',
                content: '您确定要删除吗',
                success: (result) => {
                    if (result.confirm) {
                        this.deleteRequest.run().then(() => {
                            wx.showToast({
                                title: '删除成功',
                                icon: 'none'
                            })
                            setTimeout(() => {
                                wx.navigateBack()
                            }, 1500)
                        })
                    }
                }
            })
        },
        submit() {
            if (this.data.inputValue) {
                this.reRequest.run({
                    info_id: this.data.id,
                    travel_id: this.data.id,
                    content: this.data.inputValue
                }).then(() => {
                    this.setData({
                        inputValue: ''
                    })
                    this.commentRequest.reset()
                })
            }
        },
        getBannerSize(data) {
            Promise.allSettled(data.imgs.map(item => {
                return wxPromise(wx.getImageInfo, {
                    src: item.content + (item.type == 1 ? '' : '?x-oss-process=video/snapshot,t_1,f_jpg,w_300,h_0,m_fast,ar_auto')
                })
            })).then(res => {
                return res.map(item => {
                    if (item.status === 'fulfilled') {
                        return {
                            height: item.value.height / item.value.width * windowWidth
                        }
                    } else {
                        return {
                            height: 250
                        }
                    }
                })
            }).then(res => {
                this.setData({
                    height: res[0].height
                })
                setTimeout(() => {
                    this.setData({
                        imgSize: res,
                    })
                },600)
            })
        },
    }
});
